﻿extern alias core;

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using System.IO;

using core::OmniKits.Utilities;

namespace OmniKitsCoreTest
{
    [TestClass]
    public class SecureWebEncodingTestForCore
    {
        [TestMethod]
        public void EncodeURIComponentTest()
        {
            EncodeTestCore("1.a.Ø.啊", "1.a.%C3%98.%E5%95%8A");
            EncodeTestCore("𩰪㦲𦰗𠀼 𣂕𣴑𣱳𦁚", "%F0%A9%B0%AA%E3%A6%B2%F0%A6%B0%97%F0%A0%80%BC%20%F0%A3%82%95%F0%A3%B4%91%F0%A3%B1%B3%F0%A6%81%9A");
        }

        [TestMethod]
        public void UltraLongEncodeURIComponentTest()
        {
            var sb = new StringBuilder(0xD800);
            for (var i = 0; i < 0xD800; i++)
                sb.Append((char)i);
            EncodeTestCore(sb.ToString(),
                UltraLargeTestData.ZeroToD7FF
            );

            sb = new StringBuilder(0xFFFF - 0xDFFF);
            for (var i = 0xE000; i < 0x10000; i++)
                sb.Append((char)i);
            EncodeTestCore(sb.ToString(),
                UltraLargeTestData.E000ToFFFF
            );

            sb = new StringBuilder(0xE000 - 0xD800);
            for (var i = 0xD800; i < 0xDC00; i++)
            {
                sb.Append((char)i);
                sb.Append((char)(i + 0x400));
            }
            EncodeTestCore(sb.ToString(),
                UltraLargeTestData.SurrogatePairs
            );
        }

        private void EncodeTestCore(string source, string expected)
        {
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                SecureWebEncodingUtility.SecureEncodeURIComponentForTextWriter(source.ToCharArray(), 0, null, sw);
                //source.ToCharArray().SecureEncodeURIComponentForTextWriter(0, null, sw);
            }
            Assert.AreEqual(expected, sb.ToString());
        }
    }
}
